<template>
  <div class="content">
    <el-form ref="form" :model="form" label-width="80px" :inline="true">

      <el-form-item>
        <el-input clearable v-model="form.deviceNo" size="mini" placeholder="请输入设备编号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-select
          v-model="form.schoolId"
          @change="getShiTang"
          size="mini"
          clearable
          placeholder="请选择学校"
        >
          <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.shitangId"
          @change="getShop"
          size="mini"
          clearable
          placeholder="请选择食堂"
        >
          <el-option v-for="item in shitangList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.shopId" size="mini" clearable placeholder="请选择档口">
          <el-option v-for="item in shopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="mini" class="btn" @click="search(1)">
        <i class="fa fa-refresh" style="margin-right:5px;"></i>清空
      </el-button>
      <el-button type="primary" size="mini" class="btn" @click="search(2)">
        <i class="fa fa-search" style="margin-right:5px;"></i>查询
      </el-button>
      <el-button
        type="primary"
        size="mini"
        class="btn"
        style="margin-right: 10px"
        @click="editAddOrUpdate(1)"
      >
        <i class="fa fa-plus" style="margin-right:5px;"></i>新增
      </el-button>
    </el-form>

    <!-- <el-divider></el-divider> -->

    <template>
      <el-table
        :data="tableData"
        v-loading="loading"
        size="mini"
        height="470"
        style="width: 100%"
      >
        <el-table-column prop="id" label="设备ID" width="150"></el-table-column>
        <el-table-column prop="name" width="150" label="设备名称"></el-table-column>

        <el-table-column prop="deviceNo" label="设备编号"></el-table-column>

        <el-table-column prop="name" label="所属档口"></el-table-column>

        <el-table-column prop="createTime" label="安装日期"></el-table-column>

        <el-table-column prop="limitTime" label="刷脸间隔（秒）"></el-table-column>

        <el-table-column fixed="right" :label="$t('action.operation')" width="250">
          <template slot-scope="scope">
            <!--            <el-button-->
            <!--              size="mini">{{$t('action.bangDing')}}-->
            <!--            </el-button>-->
            <el-button size="mini" @click="editAddOrUpdate(2,scope.row.id)">
              <i class="fa fa-edit" style="margin-right:5px;"></i>
              {{$t('action.edit')}}
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
              <i class="fa fa-trash" style="margin-right:5px;"></i>
              {{$t('action.delete')}}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20,50,100]"
      :page-size="everyPageNum"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!--新增编辑弹框-->
    <el-dialog center="true" :title="updateStatus==1?'创建设备':'编辑设备'" :visible.sync="showEdit" width="50%" v-dialogDrag>
      <edit @hideEdit="hideEditStatus" ref="child" :msg="updateStatus"></edit>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import edit from "@/components/DeviceManagement/edit";

export default {
  name: "editCanteen",
  components: {
    edit
  },
  data() {
    return {
      url: this.$Config.url,
      showEdit: false, //显示编辑弹框
      updateStatus: 1, //默认是新增
      total: 1, //总页数
      loading: false,
      everyPageNum: 10, //每页多少条
      currentPage: 1, //当前页
      form: {
        deviceNo: "",
        shopId: "", //档口id
        shitangId: "", //食堂id
        schoolId: "" //学校id
      },
      tableData: [],
      schoolList: [], //学校数据
      shitangList: [], //食堂数据
      shopList: [], //档口数据
      options: [
        {
          value: "选项1",
          label: "黄金糕"
        },
        {
          value: "选项2",
          label: "双皮奶"
        },
        {
          value: "选项3",
          label: "蚵仔煎"
        },
        {
          value: "选项4",
          label: "龙须面"
        },
        {
          value: "选项5",
          label: "北京烤鸭"
        }
      ]
    };
  },
  mounted() {
    this.common();
    this.getSchool();
  },
  methods: {
    handleDelete(index, row) {
      this.$confirm("您确认要删除该档口吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteDevice(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    deleteDevice(id) {
      const that = this;
      this.$axios
        .post(
          that.url + "/admin/device/delete/{id}",
          that.$qs.stringify({ id }),
          {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          }
        )
        .then(function(res) {
          if (res.data.code == 200) {
            that.$message({
              type: "success",
              message: "删除成功!"
            });
            that.common(that.currentPage, that.everyPageNum);
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    hideEditStatus(status) {
      this.showEdit = false;
      this.common(this.currentPage, this.everyPageNum);
    },
    editAddOrUpdate(status, id) {
      //新增或编辑
      /**
       * 1是新增
       * 2是编辑
       */
      this.showEdit = true;
      if (status == 1) {
        //新增
        this.updateStatus = 1;
        setTimeout(() => {
          this.$refs["child"].resetForm("form");
        }, 0);
      } else {
        //编辑
        this.updateStatus = 2;
        setTimeout(() => {
          this.$refs["child"].resetData(id);
        }, 0);
      }
    },
    getShop() {
      const that = this;
      this.$axios
        .get(that.url + "/admin/shop/list", {
          params: {
            marketId: that.form.shitangId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.shopList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    getShiTang() {
      const that = this;
      this.$axios
        .get(that.url + "/admin/market/list", {
          params: {
            deptId: that.form.schoolId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.shitangList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    getSchool() {
      const that = this;
      this.$axios
        .get(that.url + "/admin/dept/list", {
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.schoolList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    aa(arr) {
      console.log(arr);
    },
    search(index) {
      if (index == 1) {
        this.form = {
          deviceNo: "",
          shopId: "", //档口id
          shitangId: "", //食堂id
          schoolId: "" //学校id
        };
      }
      this.common();
    },
    handleSizeChange(val) {
      //   每页多少条
      this.everyPageNum = val;
      this.common(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.common(val, this.everyPageNum);
    },
    common(pageNo = 1, pageSize = 10) {
      const that = this;
      that.loading = true;
      this.$axios
        .get(that.url + "/admin/device/page", {
          params: {
            pageNo,
            pageSize,
            deviceNo: that.form.deviceNo,
            shopId: that.form.shopId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          that.loading = false;
          if (res.data.code == 200) {
            that.tableData = res.data.data.records;
            that.total = res.data.data.total;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          that.loading = false;
          console.log(error);
        });
    },
    handleClick(row) {
      console.log(row);
    }
  }
};
</script>

<style scoped lang="scss">
.table_btn {
  background: #eee;
  color: #7b9dda;
  border: none;
  outline: none;
  padding: 4px 10px;
  margin-bottom: 5px;
  border-radius: 5px;
}

.button_list {
  margin-bottom: 10px;
}

.content {
  text-align: left;
}

.el-divider {
  margin: 10px 0;
}

.btn {
  margin-top: 7px;
}

.el-form-item {
  margin-bottom: 0;
}

.el-pagination {
  float: right;
  margin: 20px 10px;
}
</style>
